#include "comm.hpp"

int main()
{
    Init init;
    // 创建共享内存
    int shmid = CreateShm();
    // 挂接共享内存
    char* shmaddr = (char*)shmat(shmid, nullptr, 0);

    int fd = open(FIFO_FILE, O_RDONLY);
    if(fd < 0)
    {
        lg(Fatal, "err code: %d, error string: %s", errno, strerror(errno));
        exit(FIFO_OPEN_ERR);
    }
    // ipc code
    struct shmid_ds shmds;
    while (true)
    {
        char c;
        ssize_t s = read(fd, &c, 1);
        if(s <= 0) break;

        std::cout << "client say# " << shmaddr << std::endl;
        // sleep(1);

        // shmctl(shmid,IPC_STAT, &shmds); // shmds输出型参数
        // std::cout << "shm nattch: " << shmds.shm_nattch << std::endl;
        // std::cout << "shm size: " << shmds.shm_segsz << std::endl;
        // std::cout << "shm mode: " << shmds.shm_perm.mode << std::endl;
        // printf("shm key: 0x%x\n", shmds.shm_perm.__key);
        // sleep(5);
    }

    // 共享内存去挂载
    shmdt(shmaddr);
    // 释放共享内存
    shmctl(shmid, IPC_RMID, nullptr);
    return 0;
}